import joi from 'joi'
import winston from 'winston'

const envVarsSechema = joi.object({

  LOGGER_LEVEL: joi.string()
    .allow(['error', 'warn', 'info', 'verbose', 'debug', 'silly'])
    .default('info'),

  LOGGER_ENABLED: joi.boolean()
    .truthy('TRUE')
    .truthy('true')
    .falsy('FALSE')
    .falsy('false')
    .default(true)

}).unknown()
  .required()

const { error, value: envVars } = joi.validate(process.env, envVarsSechema)
if (error) {
  throw new Error(`Config validation error: ${error.message}`)
}

const config = {
  logger: {
    level: envVars.LOGGER_LEVEL,
    enabled: envVars.LOGGER_ENABLED
  }
}

/**
 * When using a logger that outputs to
 * process.stdout.write
 * you can use
 * "outputCapture": "std" in the launch config to also capture that.
 */
// https://stackoverflow.com/questions/48027266/winston-logger-nodejs-debug-console-logs-not-showing-in-vscode
winston.level = config.logger.level
if (!config.logger.enabled) {
  winston.remove(winston.transports.Console)
}

module.exports = config
